//
//  HMMainViewController.swift
//  Weibo-swift
//
//  Created by GD on 16/9/19.
//  Copyright © 2016年 geduo. All rights reserved.
//

import UIKit

class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tab = HMTabBar()
        //设置撰写按钮点击的事件响应
        // 注意：此闭包内使用 `self` 会形成循环引用
        tab.composeButtonClosure = {[weak self] in
            print((self?.description)!+"我是闭包调用过来的")
        }
        tab.hmDelegate = self;
        
        setValue(tab, forKey: "tabBar")

        
        //统一设置  越早执行越好.  一般会放到APPDelegate里面
       // UITabBar.appearance().tintColor = UIColor.orange

       //添加子视图控制器 ---------------两种方式(数组,和addChildViewControllers
        addChildViewController(childController: HMHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childController: HMMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: HMDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childController: HMProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
    }
    
    func addChildViewController(childController: UIViewController,title: String, imageName: String) {
        
        childController.title = title
//        childController.tabBarItem.title = title;
        childController.tabBarItem.image = UIImage(named: imageName)//?.withRenderingMode(.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_highlighted")?.withRenderingMode(.alwaysOriginal)//修改图片的渲染方式
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        
        //创建导航
        let nav = HMNavgationViewController(rootViewController: childController)
        
        addChildViewController(nav)
    }


}

extension HMMainViewController : HMTabBarDelegate{
    func didSelectedComposeButton() {
        print((self.description)+"我是代理调用过来的")
    }
}
